home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / PowerPlant / LProgressIndicator & Friends / LThermometerPane / LThermometerPane.cp < prev    next >
Encoding:
Text File  |  1996-04-12  |  5.2 KB  |  275 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        LThermometerPane.cp
  3.  
  4.     Contains:    Thermometer indicator pane for PowerPlant.
  5.                 An LProgressIndicator subclass.
  6.                 
  7.     Version:    2.0
  8.  
  9.     Author:        Chris K. Thomas, ckt@best.com
  10.     
  11.     Copyright:    ©1995 Chris K. Thomas.  All Rights Reserved.
  12. */
  13.  
  14. #include <UMemoryMgr.h>
  15. #include <UDrawingState.h>
  16. #include <LStream.h>
  17.  
  18. #include "LThermometerPane.h"
  19. #include "WindoidUtil.h"
  20.  
  21. const short kStripesPatID = 128;
  22.  
  23. SInt32            LThermometerPane::sInstanceCount = 0;
  24. PixPatHandle    LThermometerPane::sStripesPat = NULL;
  25.  
  26. //
  27. // ——• Instances ———————————————————————————————————————————————————————————————————————
  28. //
  29.  
  30. LThermometerPane *
  31. LThermometerPane::CreateThermometerFromStream(LStream *inStream)
  32. {
  33.     return new LThermometerPane(inStream);
  34. }
  35.  
  36. LThermometerPane::LThermometerPane(LStream *inStream)
  37. :LPane(inStream)
  38. {
  39.     //
  40.     // setup pattern
  41.     //
  42.     if(sInstanceCount < 1)
  43.         sStripesPat = GetPixPat(kStripesPatID);
  44.     
  45.     sInstanceCount++;
  46.     mRight = 0;
  47.     mOriginOffset = 0;
  48.  
  49.     //
  50.     // setup default values
  51.     //
  52.  
  53.     SInt32 min, max, value;
  54.  
  55.     inStream->ReadData(&value, sizeof(SInt32));
  56.     inStream->ReadData(&min, sizeof(SInt32));
  57.     inStream->ReadData(&max, sizeof(SInt32));
  58.     
  59.     SetMinValue(min);
  60.     SetMaxValue(max);
  61.     SetCurrentValue(value);
  62.     SetTaskType(task_Measured);
  63. }
  64.  
  65. LThermometerPane::LThermometerPane(const    SPaneInfo    &inPaneInfo,
  66.                                             ETaskType    inTaskType, 
  67.                                             SInt32        inMin,
  68.                                             SInt32        inMax,
  69.                                             SInt32        inStartMax)
  70. :LProgressIndicator(inTaskType, inMin, inMax, inStartMax),
  71.  LPane(inPaneInfo)
  72. {
  73.     //
  74.     // setup pattern
  75.     //
  76.     if(sInstanceCount < 1)
  77.         sStripesPat = GetPixPat(kStripesPatID);
  78.     
  79.     sInstanceCount++;
  80.     
  81.     mRight = 0;
  82.     mOriginOffset = 0;
  83.     //
  84.     // we don't check here if this is NULL because a user may
  85.     // not in fact use the infinite stripe capability
  86.     //
  87. }
  88.  
  89. LThermometerPane::~LThermometerPane()
  90. {
  91.     //
  92.     // get rid of pattern
  93.     //
  94.  
  95.     sInstanceCount--;
  96.     
  97.     if(sInstanceCount < 1)
  98.     {
  99.         if(sStripesPat)
  100.             DisposePixPat(sStripesPat);
  101.         
  102.         sStripesPat = NULL;
  103.     }
  104. }
  105.  
  106.  
  107. //
  108. // ——• Calculations ———————————————————————————————————————————————————————————————————————
  109. //
  110.  
  111. //
  112. // • ValueChanged - we've made progress!  Let's
  113. //    update the display to reflect this!
  114. //    from LProgressIndicator
  115. //
  116.  
  117. void LThermometerPane::ValueChanged()
  118. {
  119.     if(FocusDraw())
  120.     {
  121.         Rect    r;
  122.         short    length;
  123.         
  124.         CalcLocalFrameRect(r);
  125.         
  126.         InsetRect(&r, 1, 1);
  127.         
  128.         length = r.right - r.left;
  129.  
  130.         //
  131.         // recalculate the right side of the “fill” rect
  132.         //
  133.         
  134.         if(GetTaskType() == task_Measured)
  135.         {
  136.             Assert_((mMaxValue - mMinValue) != 0);    // debug
  137.             mRight = length * mCurValue / (mMaxValue - mMinValue);
  138.         }
  139.         
  140.         DrawInside();
  141.     }
  142. }
  143.  
  144.  
  145.  
  146. //
  147. // ——• Imaging ———————————————————————————————————————————————————————————————————————
  148. //
  149.  
  150. //
  151. // •    DrawSelf - from LPane
  152. //
  153. void LThermometerPane::DrawSelf()
  154. {
  155.     DrawBox();
  156.     DrawInside();
  157. }
  158.  
  159.  
  160. //
  161. // •    DrawBox - draw our border
  162. //
  163. void LThermometerPane::DrawBox()
  164. {
  165.     Rect            r;
  166.     StColorState    currStateSavor;
  167.     
  168.     CalcLocalFrameRect(r);
  169.     
  170.     StColorState::Normalize();
  171.     
  172.     FrameRect(&r);
  173. }
  174.  
  175.  
  176. //
  177. // •    DrawInside - draw our inner self
  178. //
  179. void LThermometerPane::DrawInside()
  180. {
  181.     Rect            r;
  182.     StColorState    currStateSavor;
  183.     
  184.     CalcLocalFrameRect(r);
  185.     
  186.     StColorState::Normalize();
  187.     
  188.     InsetRect(&r, 1, 1);
  189.     
  190.     switch(mTaskType)
  191.     {
  192.         case task_Measured:
  193.         {
  194.             Rect         empty, full;
  195.             RGBColor    emptyColor, fullColor;
  196.             
  197.             empty = full = r;
  198.             empty.left = mRight + r.left + 1;    //mRight starts from port origin
  199.             full.right = mRight + r.left;
  200.             
  201.             GetWctbColor((WindowPeek)GetMacPort(), wTingeLight, &emptyColor);
  202.             GetWctbColor((WindowPeek)GetMacPort(), wTingeDark, &fullColor);
  203.             
  204.             RGBForeColor(&emptyColor);
  205.             PaintRect(&empty);
  206.             RGBForeColor(&fullColor);
  207.             PaintRect(&full);
  208.         } break;
  209.         
  210.         case task_Indeterminate:
  211.         {
  212.             DrawInfiniteStripe(r);
  213.         } break;
  214.     }
  215. }
  216.  
  217. //
  218. //    •    DrawInfiniteStripes - this pattern is used
  219. //        when there's no end in sight to the task
  220. //        As in the Finder's lengthy Find dialog.
  221. //        (hold down command-shift-F in 7.5 Finder to get
  222. //        to the original Find box to try it.)
  223. //
  224.  
  225. void LThermometerPane::DrawInfiniteStripe(Rect &inRect)
  226. {
  227.     Rect    localRect = inRect, patternRect;
  228.     
  229.     ThrowIfNULL_(sStripesPat);
  230.     
  231.     const short kOffsetAmount = 2;
  232.     
  233. //
  234. //    Munge the pixpatpalette to the user's colors
  235. //    we need to do this each time in case the user switches
  236. //    to the Finder and changes her colors.
  237. //
  238. //    This probably won’t break under System 8, but we’ll use the Appearance
  239. //    Manager then anyway, won’t we?
  240. //
  241.     
  242.     // * lock down the stripe pattern within this box
  243.     {
  244.         StHandleLocker    sta_lock((Handle)sStripesPat);
  245.         StHandleLocker    stb_lock((Handle)(**sStripesPat).patMap);
  246.         StHandleLocker    stc_lock((Handle)(**(**sStripesPat).patMap).pmTable);
  247.         
  248.         patternRect = (**(**sStripesPat).patMap).bounds;
  249.         
  250.         GetWctbColor((WindowPeek)GetMacPort(), wTingeLight, &(**(**(**sStripesPat).patMap).pmTable).ctTable[0].rgb);
  251.         GetWctbColor((WindowPeek)GetMacPort(), wTingeDark, &(**(**(**sStripesPat).patMap).pmTable).ctTable[1].rgb);
  252.     }
  253.     
  254.     OffsetRect(&localRect, mOriginOffset, 0);
  255.  
  256.     //
  257.     // offset the origin to move the pattern
  258.     //
  259.  
  260.     SetOrigin(mOriginOffset, 0);
  261.     FillCRect(&localRect, sStripesPat);
  262.     
  263.     mOriginOffset += kOffsetAmount;
  264.     
  265.     //
  266.     // if we’ve set the origin past the width of the pattern,
  267.     // return to 0
  268.     //
  269.     if(mOriginOffset > (patternRect.right - patternRect.left - 2))
  270.         mOriginOffset = 0;
  271.         
  272.     SetOrigin(0, 0);
  273. }
  274.  
  275.